package com.domcer.ultra.menu;

import acl.AclManager;
import acl.ILoader;
import application.loader.I18nLoader;
import application.loader.MessageLoader;
import com.domcer.ultra.menu.application.cache.PlayerCache;
import com.domcer.ultra.menu.application.cache.TaskCache;
import com.domcer.ultra.menu.application.loader.ConfigExtensionLoader;
import com.domcer.ultra.menu.application.loader.ListenerLoader;
import com.domcer.ultra.menu.application.loader.MenuCommandLoader;
import com.domcer.ultra.menu.application.loader.MenuFileLoader;
import common.constants.I18nKeyCoreEnum;
import common.utils.MessageUtil;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import org.bukkit.plugin.java.JavaPlugin;

import java.util.Arrays;

/**
 * @Author hang.wang
 * @Date 2025/1/30 09:37
 */
public class BootStrap extends JavaPlugin {

    public static JavaPlugin PLUGIN;

    /**
     * don't modify this method unless you insane <br>
     * {@link AclManager} is a unified method of management <br>
     * so this class need first to init <br>
     * if you want to see more, please go to {@link acl.AclCore} see implements
     */
    @Override
    public void onEnable() {
        PLUGIN = this;
        AclManager.init(this);
        this.init();
        MessageUtil.sendConsoleMessageWithPrefix(I18nKeyCoreEnum.INFO_WEBSITE, "https://ultrafunction.domcer.com");
        MessageUtil.sendConsoleMessageWithPrefix(I18nKeyCoreEnum.INFO_FOR_HELP, "https://ultrafunction.domcer.com/wiki");
        MessageUtil.sendConsoleMessageWithPrefix(I18nKeyCoreEnum.INFO_CONTACT_US, "(Email) admin@domcer.com | (Discord) https://discord.gg/2YZuDJwpMX | (QQ) 217934413");
        MessageUtil.sendConsoleMessageWithPrefix(I18nKeyCoreEnum.INFO_COPYRIGHT, "DoMCer NetWork, Inc.");
        super.onEnable();
    }

    /**
     * in order to avoid trigger bugs <br>
     * when bukkit server stop or reload, must close all player inventory at first <br>
     * then clear all task in {@link TaskCache} <br>
     * attention: clear is cancel the runnable task and clear jvm map <br>
     * see more: {@link TaskCache#stopAll()}
     */
    @Override
    public void onDisable() {
        Bukkit.getServer().getOnlinePlayers().forEach(Player::closeInventory);
        Bukkit.getServer().getOnlinePlayers().forEach(PlayerCache::clear);
        TaskCache.stopAll();
        super.onDisable();
    }

    /**
     * use {@link ILoader} to load important content <br>
     * like pipeline progress, the loading sequence is from top to bottom <br>
     * if you want to expand this function
     * for example <pre>
     * {@code public class CustomLoader implements ILoader {
     *
     *     @Override
     *     public void execute0() {
     *         // do anything you want
     *     }
     * }}
     * </pre>
     * what's more, this project have international language content <br>
     * you need put data to 'resource/i18n/messages.properties'
     */
    public void init() {
        Arrays.asList(
                new I18nLoader(),
                new MessageLoader(),
                new ConfigExtensionLoader(),
                new MenuFileLoader(),
                new MenuCommandLoader(),
                new ListenerLoader()
        ).forEach(ILoader::execute);
        MessageUtil.debug(I18nKeyCoreEnum.ALL_LOADERS_INIT_SUCCESSFUL);
    }
}
